//
//  MTMineSingleVideoPlayerController.swift
//  MT
//
//  Created by ooops on 2020/6/4.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import ZFPlayer
import YPNavigationBarTransition
import MTCategory

class MTMineSingleVideoPlayerController: BaseViewController {
    
    var model: MineInfoListModel?
    var player: ZFPlayerController!
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.player.currentPlayerManager.pause?()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        DispatchQueue.main.after(0.25) {
            self.player.currentPlayerManager.play?()
        }
    }
    
    deinit {
        self.player.stop()
        self.player = nil
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let playerManager = ZFAVPlayerManager()
        playerManager.scalingMode = ZFPlayerScalingMode.aspectFill
        
        self.player = ZFPlayerController.init(playerManager: playerManager, containerView: coverImageView)
        self.player.controlView = self.playControlView
        self.player.disableGestureTypes = [.pinch, .pan]
        self.player.isWWANAutoPlay = true

        view.backgroundColor = .black
        setupUI()
        makeConstraint()
        
        self.player.playerDidToEnd = { [weak self] asset in
            guard let weakSelf = self else { return }
            weakSelf.player.currentPlayerManager.replay?()
        }
        
        //if let m = model, let url = m.imgList?.first?.href, m.imgList?.first
        
        if let m = model, let url = m.imgList?.last?.href, let videoAsset: URL = URL(string: url) {
            self.player.assetURL = videoAsset
            DispatchQueue.main.after(0.25) {
                self.player.playTheIndex(0)
            }
        }
        
//        userAvatarImageView.mtSetImage(UserCenter.default.headerUrl, nil)
//        userNicknameLabel.text = "@ \(UserCenter.default.nickname ?? "")"
//        /// 用户信息 Label
//        userInfoLabel.text = "\(UserCenter.default.age ?? 0)岁  \(UserCenter.default.area ?? "")"
    }
    
    private func setupUI() {
        view.addSubview(coverImageView)
//        view.addSubview(userAvatarImageView)
//        view.addSubview(userInfoLabel)
//        view.addSubview(userNicknameLabel)
    }
    
    private func makeConstraint() {
        coverImageView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
//        userAvatarImageView.snp.makeConstraints { (make) in
//            make.right.equalToSuperview().offset(-9)
//            make.bottom.equalToSuperview().offset(-221)
//            make.size.equalTo(CGSize(width: 44, height: 44))
//        }
//
//        userInfoLabel.snp.makeConstraints { (make) in
//            make.bottom.equalToSuperview().offset(-23)
//            make.left.equalToSuperview().offset(10)
//        }
//
//        userNicknameLabel.snp.makeConstraints { (make) in
//            make.left.equalTo(userInfoLabel)
//            make.bottom.equalTo(userInfoLabel.snp.top).offset(-5)
//        }
    }
    
    lazy var coverImageView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFill
        imgView.isUserInteractionEnabled = true
        imgView.clipsToBounds = true
        imgView.tag = 100
        return imgView
    }()
    
    lazy var userAvatarImageView: UIImageView = {
        let imageV = UIImageView()
        imageV.layer.cornerRadius = 22
        imageV.layer.masksToBounds = true
        imageV.contentMode = .scaleAspectFill
        imageV.backgroundColor = .red
        imageV.layer.borderColor = UIColor(hex: 0xffffff).cgColor
        imageV.layer.borderWidth = 1
        return imageV
    }()
    
    /// 用户信息 Label
    lazy var userInfoLabel: UILabel = {
        let label = UILabel()
        label.font = PingFangSCMediumFont(12)
        label.textColor = .white
        return label
    }()
    
    /// nicheng
    lazy var userNicknameLabel: UILabel = {
        let label = UILabel()
        label.font = PingFangSCMediumFont(16)
        label.textColor = .white
        return label
    }()
    
    lazy var playControlView: MTHomePlayControlView = {
        let v = MTHomePlayControlView()
        return v
    }()

}

// MARK: ---------------------------------------- Overrides -------------------------------------------- 导航栏
extension MTMineSingleVideoPlayerController {
    override func yp_navigtionBarConfiguration() -> YPNavigationBarConfigurations {
        return [.backgroundStyleTransparent]
    }
    
//    override var preferredStatusBarStyle: UIStatusBarStyle {
//        return .lightContent
//    }
    
    override var prefersStatusBarHidden: Bool {
        return true
    }
}
